# Ndocs entities
$sTitle = qr/^Title:(.+)/; 
$sSection = qr/^Section:(.+)/; 
$sSub= qr/^(.+):/; 
$sList= qr/^-[ \t](.+)/;
$sDef= qr/^(.+)[ \t]-[ \t](.+)$/;
$sCodeLine= qr/^:(.+)/;
$sImage= qr/^(.*)\(see[ \t](.+)\)(.*)$/;
$sCode=qr/\(code\)/;
$sEnd=qr/\(end\)/;

# Latex header
print "\\documentclass[a4paper,oneside]{report}\n";
print "\\usepackage[T1]{fontenc}\n";
print "\\usepackage{graphicx}\n";
print "\\usepackage{charter}\n";
print "\\usepackage{verbatim}\n\n";

use enum qw(Title=-1 Normal=0 Code=1 List=2 Def=3);
# states: -1-title 0-normal line 1-code 2-list 3-def
# if state is not normal the line is not printed as is
# if state = 1 or 0 the line is printed as is
$state=Normal;

LINE: while (<>) 
{        
	if (/$sTitle/ && $state!=Code)
	{
		print "\\title{", $1, "}\n";
		print "\\author{Cedric Baudry}\n";
		print "\\date\\today\n\n";
		print "\\begin{document}\n\n";
		print "\\maketitle\n";
		print "\\newpage\n";
		print "\\tableofcontents\n";
		print "\\newpage\n\n";
		$state=Normal;
		next LINE;
	}

	if(/$sSection/ && $state!=Code)
	{
		if ($state==List)
		{
			print "\\end{itemize}\n\n";
		}
		if ($state==Def)
		{
			print "\\end{description}\n\n";
		}
		print "\\chapter{", $1, "}\n";
		$state=Normal;
		next LINE;
	}

	if(/$sSub/ && $state!=Code)
	{
		if ($state==List)
		{
			print "\\end{itemize}\n\n";
		}
		if ($state==Def)
		{
			print "\\end{description}\n\n";
		}		
		print "\\section{", $1, "}\n";
		$state=Normal;
		next LINE;
	}

	
	if(/$sEnd/ && $state==Code)
	{	
		print "\\end{verbatim}\n";
		$state=Normal;
		next LINE;
	}

	if (/$sCode/ && $state!=Code) 
	{
		if ($state==List)
		{
			print "\\end{itemize}\n\n";
		}
		if ($state==Def)
		{
			print "\\end{description}\n\n";
		}

		print "\\begin{verbatim}\n";
		$state=Code;
		next LINE;
	}

	if(/$sImage/ && $state!=Code)
	{
		if ($state==List)
		{
			print "\\end{itemize}\n\n";
		}
		if ($state==Def)
		{
			print "\\end{description}\n\n";
		}
		print "$1(see $2)$3";
		print "\\begin{figure}[!h]\n";
		print "\\includegraphics*[width=\\textwidth]{$2}\n\\caption{$2}\\end{figure}\n";
		$state=Normal;
		next LINE;
	}
	if (/$sList/ && $state!=Code)    
	{
		if ($state == Def)
		{
			print "\\end{description}\n\n";
		}
		if ($state != List)
		{
			$state=List;
			print "\\begin{itemize}\n\n";
		}
		print "\\item ", $1, "\n";	
		next LINE;				
	}
	if (/$sDef/ && $state!=Code)   
	{ 
		if ($state ==List)	
		{
			print "\\end{itemize}\n\n";
		}
		if ($state !=Def)
		{
			$state=Def;
			print "\\begin{description}\n\n";
		}
		print "\\item[", $1, "]  ", $2, "\n";
		next LINE;		
	}
	if (/$sCodeLine/ && $state!=Code)
	{
		print "\\begin{verbatim}$1\\end{verbatim}\n";
		next LINE;
	}
	if ($state==List)
	{
		print "\\end{itemize}\n\n";
		$state=Normal;
	}
	if ($state==Def)
	{
		print "\\end{description}\n\n";
		$state=Normal;
	}
	print $_;	# printing code and normal text
}

if ($state==List)
{
	print "\\end{itemize}\n\n";
}
if ($state==Def)
{
	print "\\end{description}\n\n";
}
print "\\listoffigures\n";
print "\\end{document}\n\n";